import { _decorator, Collider2D, Component, Contact2DType, IPhysics2DContact, Node, PhysicsSystem2D } from 'cc';
import { BaseSkill } from './BaseSkill';
import { GameTags } from '../constants/GameConstants';
import { EvolutionSkills } from '../config/EvolutionConfig';
import { Player } from '../entity/Player';
import { AudioSystem } from '../systems/AudioSystem';
import { AudioConfig } from '../config/AudioConfig';

const { ccclass, property } = _decorator;

@ccclass('RoarSkill')
export class RoarSkill extends BaseSkill {
    @property(Collider2D)
    private roarArea: Collider2D | null = null;

    private readonly damage: number = EvolutionSkills.roar.damage;
    private readonly radius: number = EvolutionSkills.roar.radius;

    start() {
        this.cooldown = EvolutionSkills.roar.cooldown;
        this.duration = 0.5; // 咆哮效果持续时间

        if (this.roarArea) {
            this.roarArea.on(Contact2DType.BEGIN_CONTACT, this.onRoarHit, this);
        }
    }

    protected onActivate(): void {
        if (!this.roarArea) return;
        this.roarArea.enabled = true;

        // 播放咆哮音效
        AudioSystem.instance.playEffect(AudioConfig.EFFECTS.ROAR);
    }

    protected onDeactivate(): void {
        if (!this.roarArea) return;
        this.roarArea.enabled = false;
    }

    private onRoarHit(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        if (otherCollider.tag === GameTags.ENEMY) {
            const enemy = otherCollider.getComponent(Player);
            if (enemy) {
                enemy.takeDamage(this.damage);
            }
        }
    }

    onDestroy() {
        if (this.roarArea) {
            this.roarArea.off(Contact2DType.BEGIN_CONTACT, this.onRoarHit, this);
        }
    }
} 